package cn.wolfcode.reptile.controller;

import cn.wolfcode.reptile.domain.Reptile;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/reptile")
public class ReptileController {

    @RequestMapping("/list")
    public String reptile(Model model,HttpServletRequest request) throws IOException {
        String url = request.getParameter("url");
        //2.创建一个默认的httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //3.如果是get请求那就创建一个get对象
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        //4.发起请求
        CloseableHttpResponse execute = httpClient.execute(httpGet);
        //5.获取数据
        HttpEntity entity = execute.getEntity();
        //6.打印数据
        String html = EntityUtils.toString(entity,"utf-8");

        //System.out.println(html);

        //解析html
        Document document = Jsoup.parse(html);

        Element title = document.getElementById("zs_title");
        Element xu = document.getElementById("zs_xu");
        Element content = document.getElementById("zs_content");
        Elements author = document.getElementsByClass("niandai_zuozhe");

        Reptile reptile = new Reptile();
        reptile.setContent(content.html());
        reptile.setSummary(xu.html());
        reptile.setTitle(title.html());
        reptile.setAuthor(author.html());
        model.addAttribute("reptile",reptile);

        System.out.println(title.html());
        System.out.println(xu.html());
        System.out.println(content.html());
        System.out.println(author.html());

        return "/WEB-INF/views/list.jsp";
    }
}
